package com.bootdo.system.service.impl;

import com.bootdo.common.config.Constant;
import com.bootdo.common.domain.FileDO;
import com.bootdo.common.service.FileService;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.system.dao.DeptDao;
import com.bootdo.system.dao.ScoreLogDao;
import com.bootdo.system.domain.DeptDO;
import com.bootdo.system.domain.ScoreLogDO;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.ScoreLogService;
import com.bootdo.system.vo.ScoreLogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * 扣分记录
 *
 * @author zhouxm
 */
@Service
public class ScoreLogServiceImpl implements ScoreLogService {

    @Autowired
    private ScoreLogDao logDao;

    @Autowired
    private DeptDao deptDao;

    @Autowired
    private FileService fileService;

    @Override
    public ScoreLogVO get(String id) throws Exception {
        ScoreLogVO scoreLogVO = logDao.get(id);
        if(null != scoreLogVO && StringUtils.isNotEmpty(scoreLogVO.getImgPath())) {
            List<FileDO> filelist = fileService.queryListByAttachId(scoreLogVO.getImgPath());
            scoreLogVO.setFileList(filelist);
        }
        return scoreLogVO;
    }

    @Override
    public List<ScoreLogVO> list(Map<String, Object> map) throws Exception {
        return logDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) throws Exception {
        return logDao.count(map);
    }

    @Override
    public int save(ScoreLogDO log) throws Exception {
        return logDao.save(log);
    }

    @Override
    public int update(ScoreLogDO log, UserDO userDO) throws Exception {
        Date now = new Date();
        if(Objects.equals(log.getStatus(), Constant.SCORE_STATUS_VERIFIED_PASS)) {
            ScoreLogVO score = logDao.get(log.getId());
            DeptDO dept = deptDao.get(score.getDeptId());
            if(null == dept) {
                dept.setScore(score.getScore());
            }else {
                dept.setScore(dept.getScore()+log.getScore());
            }
            deptDao.update(dept);
        }
        log.setVerifyBy(userDO.getUserId());
        log.setVerifyTime(new Date());
        log.setUpdatedBy(userDO.getUserId());
        log.setUpdatedTime(now);
        return logDao.update(log);
    }

    @Override
    public int remove(String id) throws Exception {
        return logDao.remove(id);
    }

    @Override
    public int batchRemove(String[] ids) throws Exception {
        return logDao.batchRemove(ids);
    }

    /**
     * 分值统计
     * @param map
     * @return
     */
    @Override
    public int getScore(Map<String, Object> map) {
        return logDao.getScore(map);
    }

    @Override
    public int updateScore(ScoreLogDO scoreLogDO) {
        int count = 0;
        try {
            count = logDao.update(scoreLogDO);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

}
